home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / plotting / pcgplots / gptcell.cpp < prev    next >
C/C++ Source or Header  |  1992-04-24  |  8KB  |  286 lines

  1. // GptCell.cpp -  pcGPlot cell array stuff
  2. // copyright 1992 Pittsburgh Supercomputing Center
  3. #include  "gpt.h"
  4. #include  "cgm.h" // basic display classes
  5. #include  "cgmdisp.h" // basic display classes
  6. #include  "gptDisp.h"
  7.  
  8.  // ***  pcDisplay::cells
  9.  int
  10.  pcDisplay::cells(const cellArray *inCarray)
  11.     {
  12.     HBITMAP hOldBitmap, hBitmap;
  13.     int *scaledPt;
  14.     POINT *scaledPtr;
  15.     int ret = 0;
  16.     int xMin, yMin, xMax, yMax, i;
  17.     unsigned long myWidth, myHeight;
  18.     unsigned long cellnx, cellny;
  19.     BOOL isRect = FALSE;
  20.     RECT  rr, srcRect;
  21.  
  22.     if ( !inCarray || !inCarray->corners())   return 1;
  23.  
  24.     const vdcPts *corners =  inCarray->corners();
  25.     getPts(corners, scaledPt); // get the scaled corner pts
  26.     scaledPtr = (POINT *)scaledPt;
  27. // #define DEBUG
  28.  #ifdef DEBUG
  29.     // Draw Green Rectangle on Display which cell array will cover
  30.     COLORREF RGB = RGB(0,255,0);
  31.     SetBackBrush(RGB);
  32.     SetHollowPen(RGB);
  33.     Rectangle(hdc,scaledPtr[0].x,scaledPtr[0].y,scaledPtr[1].x,scaledPtr[1].x);
  34.     Rectangle(hdcMeta,scaledPtr[0].x,scaledPtr[0].y,scaledPtr[1].x,scaledPtr[1].x) ;
  35.  #endif
  36.  
  37.     cellnx = inCarray->nx();
  38.     cellny = inCarray->ny();
  39.     ////
  40.     // figure out the upper left corner, width, height, y increases down
  41.     // use the full enclosing rectangle
  42.     // start out at the fourth point (so as to cover all 4)
  43.  
  44.     xMin = xMax = scaledPtr[0].x + scaledPtr[1].x - scaledPtr[2].x;
  45.     yMin = yMax = scaledPtr[0].y + scaledPtr[1].y - scaledPtr[2].y;
  46.     POINT ClipVert[5];
  47.     ClipVert[0] = scaledPtr[0];
  48.     ClipVert[1] = scaledPtr[2];
  49.     ClipVert[2] = scaledPtr[1];
  50.     ClipVert[3].x = xMax;
  51.     ClipVert[3].y = yMax;
  52.     ClipVert[4] = scaledPtr[0];
  53.  
  54.     HRGN clip = CreatePolygonRgn(ClipVert, 5, ALTERNATE);
  55.     for (i=0; i<3; ++i)
  56.         {
  57.         if (scaledPtr[i].x < xMin)  xMin = scaledPtr[i].x;
  58.         if (scaledPtr[i].x > xMax)  xMax = scaledPtr[i].x;
  59.         if (scaledPtr[i].y < yMin)  yMin = scaledPtr[i].y;
  60.         if (scaledPtr[i].y > yMax)  yMax = scaledPtr[i].y;
  61.         }
  62.  
  63.     // let's make sure things fit
  64.     if (xMin < 0) xMin = 0;
  65.     if (yMin < 0) yMin = 0;
  66.     if (xMax > width())     xMax = width();
  67.     if (yMax > height())    yMax = height();
  68.  
  69.     myWidth  = xMax - xMin;
  70.     myHeight = yMax - yMin;
  71.     if (myWidth == abs((int)(scaledPtr[1].x - scaledPtr[0].x)) &&
  72.                             myHeight  == abs((int)(scaledPtr[1].y - scaledPtr[0].y)) )
  73.         isRect = TRUE;
  74.     // shift the scaled Ptr for the cellarray
  75.     for (i=0; i<3; ++i) { scaledPtr[i].x -= xMin; scaledPtr[i].y -= yMin; }
  76.  
  77.     srcRect.top  = 0;
  78.     srcRect.left = 0;
  79.  
  80.     if (isRect && ( cellnx*cellny < myWidth *myHeight ) )
  81.         {
  82.         hBitmap = CreateCompatibleBitmap(hdc, cellnx, cellny);
  83.         srcRect.right  = cellnx;
  84.         srcRect.bottom = cellny;
  85.         }
  86.     else
  87.         {
  88.         hBitmap = CreateCompatibleBitmap(hdc, myWidth, myHeight);
  89.         srcRect.right  = myWidth;
  90.         srcRect.bottom = myHeight;
  91.         }
  92.  
  93.      HDC hMemoryDC = CreateCompatibleDC(hdc);
  94.      hOldBitmap = SelectObject( hMemoryDC, hBitmap);
  95.      if (cgmWin->hPal)
  96.         {
  97.         HPALETTE foo = SelectPalette(hMemoryDC,cgmWin->hPal,0);
  98.         int snow = RealizePalette(hMemoryDC);
  99.         }
  100.  
  101.      // Fill Bitmap in with background color
  102.      LOGBRUSH lBrush;
  103.      lBrush.lbStyle = BS_SOLID;
  104.      lBrush.lbColor = RGBbackground;
  105.      HBRUSH hBrush = CreateBrushIndirect((LOGBRUSH FAR *)&lBrush);
  106.      //HBRUSH hOldBrush = SelectObject(hdc, (HANDLE)hBrush);
  107.      //SelectObject(hdcMeta, (HANDLE)hBrush);
  108.      //PatBlt(hMemoryDC,0,0,srcRect.right, srcRect.bottom,PATCOPY);
  109.      //SelectObject(hMemoryDC, (HANDLE)hOldBrush);
  110.      //DeleteObject( hBrush);
  111.  
  112.              HBRUSH hOldBrush = SelectObject(hMemoryDC, (HANDLE)hBrush);
  113.              SelectObject(hMemoryDC, (HANDLE)hBrush);
  114.              PatBlt(hMemoryDC,0,0,srcRect.right, srcRect.bottom,PATCOPY);
  115.              SelectObject(hMemoryDC, (HANDLE)hOldBrush);
  116.              DeleteObject( hBrush);
  117.  
  118.  
  119.      //  do fill for pc
  120.      inCarray->fill( scaledPtr, myWidth, myHeight, hMemoryDC );
  121.      //for (int xInd = 0; xInd < srcRect.right; xInd +=3)
  122.      //     for (int yInd = 0; yInd < srcRect.bottom; yInd +=3)
  123.      //        SetPixel(hMemoryDC,xInd, yInd, RGB(255,0,0));
  124.  
  125.     SelectObject( hdc, clip);
  126.     //SelectObject(hdcMeta,clip);
  127.     DeleteObject( clip);
  128.     StretchBlt(hdc, xMin, yMin,xMax -xMin, yMax-yMin, hMemoryDC,0,0, srcRect.right,
  129.                  srcRect.bottom, SRCCOPY);
  130.     StretchBlt(hdcMeta, xMin, yMin,xMax -xMin, yMax-yMin, hMemoryDC,0,0, srcRect.right,
  131.                  srcRect.bottom, SRCCOPY);
  132.  
  133.     SelectObject(hMemoryDC, hOldBitmap);
  134.     DeleteDC(hMemoryDC);
  135.     DeleteObject(hBitmap);
  136.  
  137.  
  138.     // Restore original clip
  139.     ClipVert[0].x = clipr.left;
  140.     ClipVert[0].y = clipr.top;
  141.     ClipVert[1].x = clipr.right;
  142.     ClipVert[1].y = clipr.top;
  143.     ClipVert[2].x = clipr.right;
  144.     ClipVert[2].y = clipr.bottom;
  145.     ClipVert[3].x = clipr.left;
  146.     ClipVert[3].y = clipr.bottom;
  147.     ClipVert[4]   = ClipVert[0];
  148.     clip = CreatePolygonRgn(ClipVert, 5, ALTERNATE);
  149.     SelectClipRgn(hdc, clip);
  150.     //SelectClipRgn(hdcMeta, clip);
  151.     DeleteObject(clip);
  152.  
  153.     delete scaledPt;
  154.     return 0;
  155.     }
  156.  
  157.  
  158.  
  159.  
  160. // cell array routine to fill out cell array bitmap
  161. //  *** cellArray::fill
  162.  void
  163.  cellArray::fill(POINT *inPts, unsigned int inWidth, unsigned int inHeight
  164.                     , HDC hdcMem) const
  165.     {
  166.     const unsigned long PAL = 0X2000000 ;
  167.  
  168.     long ix, iy, newX, newY, oldX, oldY, cellX, cellY;
  169.     COLORREF myPixelColor;
  170.     int topX, topY;
  171.     float *myColrs, rx, ry;
  172.     double axx, axy, ayx, ayy;  // xform matrix from X space to cell array space
  173.     long Qx, Qy, Rx,Ry;
  174.     long det;
  175.     double ddet;
  176.     DWORD stat;
  177.     BOOL isRect1, isRect2;
  178.     BOOL GotColor = FALSE;
  179.     BOOL SetColor = FALSE;
  180.  
  181.     cellX = this->myNx;
  182.     cellY = this->myNy;
  183.     isRect1 = isRect2 = FALSE;
  184.     // get the offsets from the first co-ord, P
  185.     Qx = inPts[1].x - inPts[0].x;
  186.     Qy = inPts[1].y - inPts[0].y;
  187.     Rx = inPts[2].x - inPts[0].x;
  188.     Ry = inPts[2].y - inPts[0].y;
  189.  
  190.     // See if rectangular and no floating point cell array - memory xformation needed
  191.     if ( (Rx == 0 && Ry == Qy) || ( Ry == 0 && Rx == Qx))
  192.         {
  193.         if (  cellX*cellY <= (long)((long)inHeight*inWidth))
  194.              isRect1 = TRUE;     // filling in cellX by cellY memory size
  195.         else isRect2 = TRUE;    // filling in inWidth by inHeight memory size
  196.         }
  197.     else
  198.         {
  199.         // now the determinant of the transformation
  200.         det = Rx * Qy - Ry * Qx;
  201.         ddet = (double) det; // take care of ULTRIX C bug
  202.  
  203.         if (!det) return; // P, Q, and R are co-linear ( gads!!)
  204.         // now the actual matrix components
  205.         axx = (cellX * (Qy - Ry) )/ ddet;
  206.         axy = (cellX * (Rx - Qx) ) / ddet;
  207.         ayx = - (cellY * Ry )/ ddet;
  208.         ayy = (cellY * Rx )/ ddet;
  209.         }
  210.  
  211.     if (isRect1)   {  topY = cellY; topX = cellX; }
  212.     else           {  topY = inHeight; topX = inWidth; }
  213.     //if (topY > 100) topY =100;
  214.     //if (topX > 100) topX = 100;
  215.     for (iy=0; iy < topY; ++iy)
  216.         {
  217.         if (isRect1)
  218.             {
  219.             newY = iy;
  220.             if (Qy < 0) newY = cellY - newY -1;
  221.             }
  222.         else if (isRect2)
  223.             {
  224.             oldY = iy - inPts[0].y;
  225.             newY = (oldY * cellY)/Qy;
  226.             }
  227.         else
  228.             oldY = iy - inPts[0].y;
  229.  
  230.         for (ix=0; ix < topX; ++ix)
  231.             {
  232.             if (isRect1)
  233.                 {
  234.                 newX = ix;
  235.                 if (Qx < 0) newX = cellX - newX -1;
  236.                 }
  237.             else if (isRect2)
  238.                 {
  239.                 oldX = ix - inPts[0].x;
  240.                 newX = (oldX * cellX)/Qx;
  241.                 }
  242.             else
  243.                 {
  244.                 oldX = ix - inPts[0].x;
  245.                 rx = axx * oldX + axy * oldY;
  246.                 ry = ayx * oldX + ayy * oldY;
  247.                 newX = (int) rx;
  248.                 newY = (int) ry;
  249.                 }
  250.  
  251.             // is rectangular or the transformed pt in the affected area ?
  252.             if ( isRect1 || isRect2 || ((rx >= 0)
  253.                  && (rx < cellX) && (ry >= 0) && (ry < cellY)))
  254.                 {
  255.                 myColrs = getFloats(newX, newY);
  256.                 myPixelColor = RGB(    (BYTE)(myColrs[0] * 0XFF),
  257.                                               (BYTE)(myColrs[1] * 0XFF),
  258.                                               (BYTE)(myColrs[2] * 0XFF)
  259.                                                 ) | PAL;
  260.               if (myPixelColor & 0XFFFFFF)
  261.                      {
  262.                      GotColor = TRUE;
  263.                      }
  264.                 stat = SetPixel(hdcMem,ix, iy, myPixelColor);
  265.                 if (stat)
  266.                   {
  267.                     SetColor = TRUE;;
  268.                     }
  269.                 }
  270.             }
  271.         }
  272.     int moo = 0;  // for debug stop
  273.     }
  274.  
  275.  
  276.  //  *********************** set_rect() **************************
  277. RECT *set_rect(RECT *rectangle, short top, short left,
  278.                     short bottom, short right)
  279.   {
  280.   rectangle->top    = top;
  281.   rectangle->left   =left;
  282.   rectangle->bottom = bottom;
  283.   rectangle->right  = right;
  284.   return rectangle;
  285.   }
  286.